package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.HorderDao;
import dao.HotelDao;
import dao.RorderDao;
import model.Horder;
import model.Rorder;
import model.Tourist;
import util.CharSet;

/**
 * Servlet implementation class HotelServlet
 */
@WebServlet("/HorderServlet")
public class HorderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HorderServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String handle = request.getParameter("handle");
		if (handle != null && handle.equals("addHorder")) {
			String hemail = CharSet.getNewString(request.getParameter("hemail"));
			HttpSession session = request.getSession();
			Tourist tourist = (Tourist) session.getAttribute("tourist");
			String temail = tourist.getEmail();
			try {

				Horder horder = HorderDao.newHorder(temail, hemail);
				int sum = HotelDao.getHotelNoCache(hemail).getSum();
				if ((!HorderDao.getHorderNoCacheForBool(horder)) && sum >= 1) {

					HorderDao.addHorder(horder);

					String flag = "1";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("HotelList.jsp").forward(request, response);
				} else {
					// System.out.println("ggg");
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("HotelList.jsp").forward(request, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
				// System.out.println("JJJ");
				String flag = "0";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("HotelList.jsp").forward(request, response);
			}

		}
		if (handle != null && handle.equals("delHorder")) {
			String hemail = CharSet.getNewString(request.getParameter("hemail"));
			HttpSession session = request.getSession();
			Tourist tourist = (Tourist) session.getAttribute("tourist");
			String temail = tourist.getEmail();
			Horder horder;
			try {
				horder = HorderDao.newHorder(temail, hemail);
				if (HorderDao.getHorderNoCacheForBool(horder)) {
					HorderDao.delHorder(horder);
					String flag = "1";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("MyOrder.jsp").forward(request, response);
				} else {
					// System.out.println("ggg");
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("MyOrder.jsp").forward(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
